<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;


class GroupController extends BaseController{
    public function add(){
        $this->assign('url',U('Group/insert'));
        $this->assign('title','角色添加');
        $this->assign('info','添加');
        $this->display('Group_info');
    }
    public function insert(){
        $groupinfo=D('Auth_group')->create();
        $addinfo=D('Auth_group')->add($groupinfo);
        if ($addinfo){
            $this->success('添加成功！',U('Group/View'));
        }else {
            $this->error('添加失败！');
        }
    }
    
    public function edit(){
        $this->assign('title','角色更新');
        $this->assign('url',U('Group/update'));
        $this->assign('editinfo',D('Group')->editInfo());
        $this->assign('info','更新');
        $this->display('Group_info');
    }
    public function update(){
        $result=D('Group')->updateInfo();
        if (false !== $result || 0 !== $result){
            $this->success('更新成功！',U('Group/view'));
        }else {
            $this->error('更新失败！');
        }
    }
    
    public function delete(){
        if(D('Group')->delInfo()){
            $data['state']=1;
            $data['msg']='删除成功';
            $this->ajaxReturn($data);
        }else {
            $data['state']=0;
            $data['msg']='删除失败';
            $this->ajaxReturn($data);
        }
    }
    
   public function authority(){     
       $map['id']=I('get.id');
       $map['title']=I('get.name');
       $groupname=D('Auth_group')->where($map)->find();
       $this->assign('groupname',$groupname);      
       $rulelist=D('Auth_rule')->field('id,title')->order('convert(title using utf8) asc')->select();       
       $ruleid=D('Auth_group')->field('rules')->where($map)->select();       
       $this->assign('ruleid',$ruleid);
       $this->assign('title','授权页面');
       $this->assign('rulelist',$rulelist); 
       $this->assign('url',U('Group/updaterule'));
       $this->display('Auth_list');
       
   }
   
   public function updaterule(){
       $arr=I('post.rules');  
       $where['id']=I('post.id');
       $data['rules']=implode(',',$arr);
       $m=M('Auth_group');
       $num=$m->where($where)->save($data);
       if(false !== $num || 0 !== $num){
           $this->success('权限更新成功!');
       }else{
           $this->error('权限更新失败!');
       }      
   }
    
    public function View(){
        $this->assign('title','角色列表');
        $this->assign('info','添加');
        $this->assign('grouplist',D('Auth_group')->select());
        $this->display('Group_list');
    }
}